<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【前端换肤】前端换肤的N种方案，请收下 | Vixcity</title><meta name="keywords" content="HTML,前端换肤"><meta name="author" content="Vixcity"><meta name="copyright" content="Vixcity"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前端换肤的N种方案，请收下网站换肤的需求，也就是主题切换，该如何切换主题的颜色呢？ 以下是网站换肤的实现以及基于换肤拓展的一些方案分享给大家，希望大家在做类似需求的时候能够有些参考。 覆盖样式实现 123456789101112&#x2F;&#x2F; light$color-brand1: #ffcd32;$fill-1: #fff !default;$color-text: #3c3c3c;$color-text">
<meta property="og:type" content="article">
<meta property="og:title" content="【前端换肤】前端换肤的N种方案，请收下">
<meta property="og:url" content="http://vixcity.gitee.io/my-gitee-blog/2021/10/09/%E3%80%90%E5%89%8D%E7%AB%AF%E6%8D%A2%E8%82%A4%E3%80%91%E5%89%8D%E7%AB%AF%E6%8D%A2%E8%82%A4%E7%9A%84N%E7%A7%8D%E6%96%B9%E6%A1%88%EF%BC%8C%E8%AF%B7%E6%94%B6%E4%B8%8B/index.html">
<meta property="og:site_name" content="Vixcity">
<meta property="og:description" content="前端换肤的N种方案，请收下网站换肤的需求，也就是主题切换，该如何切换主题的颜色呢？ 以下是网站换肤的实现以及基于换肤拓展的一些方案分享给大家，希望大家在做类似需求的时候能够有些参考。 覆盖样式实现 123456789101112&#x2F;&#x2F; light$color-brand1: #ffcd32;$fill-1: #fff !default;$color-text: #3c3c3c;$color-text">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://vixcity.gitee.io/my-gitee-blog/img/%E8%9C%82%E7%AA%9D%E5%BD%A2%E5%85%AD%E8%BE%B9%E5%BD%A2%E8%93%9D%E7%B4%AB%E8%89%B2%E8%B0%83%E7%82%AB%E9%85%B7%E8%83%8C%E6%99%AF.jpg">
<meta property="article:published_time" content="2021-10-08T16:00:00.000Z">
<meta property="article:modified_time" content="2022-01-04T03:04:47.337Z">
<meta property="article:author" content="Vixcity">
<meta property="article:tag" content="HTML">
<meta property="article:tag" content="前端换肤">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://vixcity.gitee.io/my-gitee-blog/img/%E8%9C%82%E7%AA%9D%E5%BD%A2%E5%85%AD%E8%BE%B9%E5%BD%A2%E8%93%9D%E7%B4%AB%E8%89%B2%E8%B0%83%E7%82%AB%E9%85%B7%E8%83%8C%E6%99%AF.jpg"><link rel="shortcut icon" href="/my-gitee-blog/img/favicon.ico"><link rel="canonical" href="http://vixcity.gitee.io/my-gitee-blog/2021/10/09/%E3%80%90%E5%89%8D%E7%AB%AF%E6%8D%A2%E8%82%A4%E3%80%91%E5%89%8D%E7%AB%AF%E6%8D%A2%E8%82%A4%E7%9A%84N%E7%A7%8D%E6%96%B9%E6%A1%88%EF%BC%8C%E8%AF%B7%E6%94%B6%E4%B8%8B/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-gitee-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-gitee-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-gitee-blog/search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: '/my-gitee-blog/',
      css: '/my-gitee-blog/'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【前端换肤】前端换肤的N种方案，请收下',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-01-04 11:04:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/my-gitee-blog/css/mycss.css"><link rel="stylesheet" href="/my-gitee-blog/css/newStyle.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome-animation@1.1.1/css/font-awesome-animation.css"><link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><link rel="stylesheet" href="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-gitee-blog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-gitee-blog/img/%E8%9C%82%E7%AA%9D%E5%BD%A2%E5%85%AD%E8%BE%B9%E5%BD%A2%E8%93%9D%E7%B4%AB%E8%89%B2%E8%B0%83%E7%82%AB%E9%85%B7%E8%83%8C%E6%99%AF.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-gitee-blog/">Vixcity</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【前端换肤】前端换肤的N种方案，请收下</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-10-08T16:00:00.000Z" title="发表于 2021-10-09 00:00:00">2021-10-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-04T03:04:47.337Z" title="更新于 2022-01-04 11:04:47">2022-01-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/">大前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/HTML/">HTML</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【前端换肤】前端换肤的N种方案，请收下"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="前端换肤的N种方案，请收下"><a href="#前端换肤的N种方案，请收下" class="headerlink" title="前端换肤的N种方案，请收下"></a>前端换肤的N种方案，请收下</h2><p>网站换肤的需求，也就是主题切换，该如何切换主题的颜色呢？</p>
<p>以下是网站换肤的实现以及基于换肤拓展的一些方案分享给大家，希望大家在做类似需求的时候能够有些参考。</p>
<h3 id="覆盖样式实现"><a href="#覆盖样式实现" class="headerlink" title="覆盖样式实现"></a>覆盖样式实现</h3><hr>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">// light</span><br><span class="line">$<span class="attribute">color</span>-brand1: <span class="number">#ffcd32</span>;</span><br><span class="line">$fill-<span class="number">1</span>: <span class="number">#fff</span> !default;</span><br><span class="line">$<span class="attribute">color</span>-text: <span class="number">#3c3c3c</span>;</span><br><span class="line">$<span class="attribute">color</span>-text-<span class="number">1</span>: <span class="number">#757575</span>;</span><br><span class="line">$<span class="attribute">color</span>-text-<span class="number">2</span>: <span class="number">#222</span>;</span><br><span class="line"></span><br><span class="line">// dark</span><br><span class="line">$dark-fill-<span class="number">1</span>: <span class="number">#222</span> !default; // 品牌色</span><br><span class="line">$dark-<span class="attribute">color</span>-text: <span class="number">#fff</span>;</span><br><span class="line">$dark-<span class="attribute">color</span>-text-<span class="number">1</span>: <span class="built_in">rgba</span>(<span class="number">255</span>, <span class="number">255</span>, <span class="number">255</span>, <span class="number">0.3</span>);</span><br><span class="line">$dark-<span class="attribute">color</span>-text-<span class="number">2</span>: $color-brand1;</span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">// 页面使用</span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">lang</span>=<span class="string">&quot;scss&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css"> <span class="keyword">@import</span> <span class="string">&quot;./assets/scss/index.scss&quot;</span>;</span></span><br><span class="line"><span class="css"> <span class="selector-attr">[data-theme=<span class="string">&quot;dark&quot;</span>]</span> &#123; </span></span><br><span class="line"><span class="css">   <span class="selector-tag">body</span> &#123;</span></span><br><span class="line"><span class="css">     <span class="attribute">background</span>: $dark-fill-<span class="number">1</span>; </span></span><br><span class="line"><span class="css">   &#125;</span></span><br><span class="line"><span class="css">  </span></span><br><span class="line"><span class="css">   <span class="selector-class">.reaconmend</span> <span class="selector-class">.reaconmend-list</span> <span class="selector-class">.item</span> <span class="selector-class">.name</span> &#123;</span></span><br><span class="line"><span class="css">     <span class="attribute">color</span>: $dark-color-text;</span></span><br><span class="line"><span class="css">   &#125;</span></span><br><span class="line"><span class="css">  </span></span><br><span class="line"><span class="css">   <span class="selector-class">.reaconmend</span> <span class="selector-class">.reaconmend-list</span> <span class="selector-class">.item</span> <span class="selector-class">.desc</span> &#123;</span></span><br><span class="line"><span class="css">     <span class="attribute">color</span>: $dark-color-text-<span class="number">1</span>;</span></span><br><span class="line"><span class="css">   &#125;</span></span><br><span class="line"><span class="css">  </span></span><br><span class="line"><span class="css">   <span class="selector-class">.header</span> <span class="selector-class">.text</span> &#123;</span></span><br><span class="line"><span class="css">     <span class="attribute">color</span>: $dark-color-text-<span class="number">2</span>; </span></span><br><span class="line"><span class="css">   &#125;</span></span><br><span class="line"><span class="css"> &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>利用css优先级的原理覆盖掉原有样式的实现，每定义一套皮肤就要定义对应的sass变量，以及定义一套覆盖原有样式的皮肤样式</p>
<p>如果有多套皮肤的话，覆盖的代码量就会n套</p>
<p><strong>缺点：</strong> 样式不易管理，查找样式复杂，开发效率低，拓展性差，维护成本高，多人协作沟通麻烦。</p>
<h3 id="sass变量实现"><a href="#sass变量实现" class="headerlink" title="sass变量实现"></a>sass变量实现</h3><hr>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// variable.scss </span></span><br><span class="line"><span class="comment">// 浅色</span></span><br><span class="line"><span class="variable">$colors-light</span>: (</span><br><span class="line">  fill-<span class="number">1</span>: <span class="number">#fff</span>,</span><br><span class="line">  text: <span class="number">#3c3c3c</span>, </span><br><span class="line">  text-<span class="number">1</span>: <span class="number">#757575</span>,</span><br><span class="line">  text-<span class="number">2</span>: <span class="number">#222</span>,</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 深色</span></span><br><span class="line"><span class="variable">$colors-dark</span>: (</span><br><span class="line">  fill-<span class="number">1</span>: <span class="number">#222</span>,</span><br><span class="line">  text: <span class="number">#fff</span>,</span><br><span class="line">  text-<span class="number">1</span>: rgba(<span class="number">255</span>, <span class="number">255</span>, <span class="number">255</span>, <span class="number">0.3</span>),</span><br><span class="line">  text-<span class="number">2</span>: <span class="number">#ffcd32</span>,</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// mixin.scss</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 背景色</span></span><br><span class="line"><span class="keyword">@mixin</span> bg-color(<span class="variable">$key</span>) &#123;</span><br><span class="line">  <span class="attribute">background-color</span>: map-get(<span class="variable">$colors-light</span>, <span class="variable">$key</span>);</span><br><span class="line">  <span class="selector-attr">[data-theme=<span class="string">&quot;dark&quot;</span>]</span> &amp; &#123;</span><br><span class="line">    <span class="attribute">background-color</span>: map-get(<span class="variable">$colors-dark</span>, <span class="variable">$key</span>); </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// text色</span></span><br><span class="line"><span class="keyword">@mixin</span> text-color(<span class="variable">$key</span>) &#123;</span><br><span class="line">  <span class="attribute">color</span>: map-get(<span class="variable">$colors-light</span>, <span class="variable">$key</span>);</span><br><span class="line">  <span class="selector-attr">[data-theme=<span class="string">&quot;dark&quot;</span>]</span> &amp; &#123;</span><br><span class="line">    <span class="attribute">color</span>: map-get(<span class="variable">$colors-dark</span>, <span class="variable">$key</span>); </span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">// 页面使用</span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">lang</span>=<span class="string">&quot;scss&quot;</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet/scss&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css"> <span class="keyword">@import</span> <span class="string">&quot;../../../assets/scss/variable.scss&quot;</span>;</span></span><br><span class="line"><span class="css"> <span class="keyword">@import</span> <span class="string">&quot;../../../assets/scss/mixin.scss&quot;</span>;</span></span><br><span class="line"><span class="css"> <span class="selector-class">.reaconmend-list</span> &#123;</span></span><br><span class="line"><span class="css">   <span class="selector-class">.list-title</span> &#123;</span></span><br><span class="line"><span class="css">     <span class="attribute">height</span>: <span class="number">40px</span>;</span></span><br><span class="line"><span class="css">     <span class="attribute">line-height</span>: <span class="number">40</span> px;</span></span><br><span class="line"><span class="css">     <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="css">     <span class="keyword">@include</span> text-color(text-<span class="number">1</span>);</span></span><br><span class="line"><span class="css">   &#125;</span></span><br><span class="line"><span class="css"> &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>如上所示用到的知识点包含Sass变量（variable），嵌套（nestend rules），混合（mixins）， Sass Maps的函数-map-get(key)。</p>
<blockquote>
<p>Maps的含义：Maps可视为键值对的集合，键被用于定位值 在css种没有对应的概念。和Lists不同Maps必须被圆括号包围，键值对被都好分割 。Maps中的keys和values可以是sassscript的任何对象。（包括任意的sassscript表达式 arbitrary SassScript expressions） 和Lists一样Maps主要为sassscript函数服务，如 map-get函数用于查找键值，map-merge函数用于map和新加的键值融合，@each命令可添加样式到一个map中的每个键值对。Maps可用于任何Lists可用的地方，在List函数中 Map会被自动转换为List ， 如 (key1: value1, key2: value2)会被List函数转换为 key1 value1, key2 value2 ，反之则不能。</p>
</blockquote>
<h4 id="使用scss变量换肤相比覆盖样式"><a href="#使用scss变量换肤相比覆盖样式" class="headerlink" title="使用scss变量换肤相比覆盖样式"></a><strong>使用scss变量换肤相比覆盖样式</strong></h4><ul>
<li><p>拓展性更强</p>
</li>
<li><p>将换肤的逻辑进行了收敛</p>
</li>
</ul>
<h3 id="生成多套皮肤css"><a href="#生成多套皮肤css" class="headerlink" title="生成多套皮肤css"></a>生成多套皮肤css</h3><hr>
<p>使用覆盖样式实现与scss变量实现会把多套皮肤的样式都编译到一个css文件里面，如果有多套皮肤样式，这个文件是会非常大的。为了解决这样的问题，就自然的想出了拆分scss的实现：</p>
<p>实现方案，通过编译工具与构建工具编译出多套皮肤css，通过js动态的link对应的皮肤样式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// js动态处理</span></span><br><span class="line"><span class="keyword">var</span> theme = <span class="regexp">/\bt=(\w+)/</span>.exec(location.search);</span><br><span class="line">theme = theme ? theme[<span class="number">1</span>] : <span class="string">&quot;light&quot;</span>;</span><br><span class="line">changeTheme(theme);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">changeTheme</span>(<span class="params">theme</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> head = <span class="built_in">document</span>.getElementsByTagName(<span class="string">&quot;head&quot;</span>)[<span class="number">0</span>];</span><br><span class="line">  <span class="keyword">var</span> link = <span class="built_in">document</span>.createElement(<span class="string">&quot;link&quot;</span>); </span><br><span class="line">  link.dataset.type = <span class="string">&quot;theme&quot;</span>;</span><br><span class="line">  link.href = <span class="string">&quot;assets/css/theme-&quot;</span> + theme + <span class="string">&quot;/pages/home/home.css&quot;</span>;</span><br><span class="line">  link.rel = <span class="string">&quot;stylesheet&quot;</span>; </span><br><span class="line">  link.type = <span class="string">&quot;text/css&quot;</span>;</span><br><span class="line">  head.appendChild(link);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="CSS变量实现"><a href="#CSS变量实现" class="headerlink" title="CSS变量实现"></a>CSS变量实现</h3><hr>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// variable.scss</span></span><br><span class="line"><span class="comment">// 默认变量:</span></span><br><span class="line">root &#123;</span><br><span class="line">  --fill-1: <span class="number">#fff</span>;</span><br><span class="line">  --text: <span class="number">#3c3c3c</span>;</span><br><span class="line">  --text-1: <span class="number">#757575</span>;</span><br><span class="line">  --text-2: <span class="number">#222</span>;</span><br><span class="line">  --<span class="attribute">font-size</span>-large: <span class="number">18px</span>;</span><br><span class="line">  --<span class="attribute">font-size</span>-large-x: <span class="number">22px</span>;</span><br><span class="line">  --<span class="attribute">font-size</span>-medium: <span class="number">14px</span>;</span><br><span class="line">  --<span class="attribute">font-size</span>-medium-x: <span class="number">16px</span>;</span><br><span class="line">  --<span class="attribute">font-size</span>-small-s: <span class="number">10px</span>;</span><br><span class="line">  --<span class="attribute">font-size</span>-small: <span class="number">12px</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 深色变量</span></span><br><span class="line"><span class="selector-attr">[data-theme=<span class="string">&quot;dark&quot;</span>]</span> &#123;</span><br><span class="line">  --fill-1: <span class="number">#222</span>;</span><br><span class="line">  --text: <span class="number">#fff</span>;</span><br><span class="line">  --text-1: rgba(<span class="number">255</span>, <span class="number">255</span>, <span class="number">255</span>, <span class="number">0.3</span>);</span><br><span class="line">  --text-2: <span class="number">#ffcd32</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在页面对css变量做引入使用</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 页面使用</span></span><br><span class="line"><span class="keyword">@import</span> <span class="string">&quot;../../assets/scss/variable.scss&quot;</span> </span><br><span class="line"> ;<span class="selector-class">.header</span> &#123;  </span><br><span class="line">   <span class="attribute">position</span>: relative;</span><br><span class="line">   <span class="attribute">height</span>: <span class="number">70px</span>;</span><br><span class="line">   <span class="attribute">text-align</span>: center;</span><br><span class="line">   <span class="attribute">font-size</span>: <span class="number">0</span>;</span><br><span class="line">   <span class="selector-class">.text</span> &#123;</span><br><span class="line">     <span class="attribute">display</span>: inline-block; </span><br><span class="line">     <span class="attribute">vertical-align</span>: top;</span><br><span class="line">     <span class="attribute">line-height</span>: <span class="number">70px</span>; </span><br><span class="line">     <span class="attribute">font-size</span>: var(--font-size-large);</span><br><span class="line">     <span class="attribute">color</span>: var(--text-<span class="number">2</span>); </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>具体的实现效果：<img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091340872.gif" alt="图片"></p>
<blockquote>
<p>问题点：css变量会存在兼容性问题</p>
</blockquote>
<p>css变量兼容性如下：<img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091340862.webp" alt="图片">虽然现在大部分主流浏览器都可以兼容，但是还要考虑更多的兼容性这块的请往下看：</p>
<h3 id="CSS变量兼容性实现-1"><a href="#CSS变量兼容性实现-1" class="headerlink" title="CSS变量兼容性实现-1"></a>CSS变量兼容性实现-1</h3><hr>
<blockquote>
<p>在css变量的基础上新增了postcss-custom-properties这个插件</p>
<p>安装依赖：npm install postcss-custom-properties –save-dev npm install postcss-loader –save-dev</p>
</blockquote>
<p>在根目录新建postcss.config.js增加配置，配置如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> postcssCustompProperties = <span class="built_in">require</span>(<span class="string">&quot;postcss-custom-properties&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  <span class="attr">plugins</span>: [ </span><br><span class="line">    postcssCustompProperties(&#123;</span><br><span class="line">      <span class="attr">importFrom</span>: <span class="string">&quot;src/assets/scss/variable.scss&quot;</span>    </span><br><span class="line">    &#125;) </span><br><span class="line">  ]</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>postcss 会将css自定义变量直接编译为<code>确定值</code>，而<code>不是保留</code></p>
<p>这时就需要 <code>postcss 插件</code> 来为我们保留这些自定义变量，使用 postcss-custom-properties效果如下：<img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091341447.webp" alt="图片"></p>
<ul>
<li><p>优点：会生成一套与css变量对应的css</p>
</li>
<li><p>缺点：在构建时根据css变量生成对应的css，换肤是运行时并不能生成对应的css。</p>
</li>
</ul>
<p>换肤后样式：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091341552.webp" alt="图片"></p>
<h3 id="CSS变量兼容性实现-2"><a href="#CSS变量兼容性实现-2" class="headerlink" title="CSS变量兼容性实现-2"></a>CSS变量兼容性实现-2</h3><hr>
<p>首先需要建一个存放公共css变量的js文件，将需要定义的css变量存放到该js文件，例如（variable.js）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// variable.js</span></span><br><span class="line"><span class="comment">// 字体变量</span></span><br><span class="line"><span class="keyword">const</span> baseSize = &#123;</span><br><span class="line"> <span class="string">&quot;--font-size-large-x&quot;</span>: <span class="string">&quot;22px&quot;</span>, </span><br><span class="line">  <span class="string">&quot;--font-size-large&quot;</span>: <span class="string">&quot;18px&quot;</span>, </span><br><span class="line">  <span class="string">&quot;--font-size-medium&quot;</span>: <span class="string">&quot;14px&quot;</span>, </span><br><span class="line">  <span class="string">&quot;--font-size-medium-x&quot;</span>: <span class="string">&quot;16px&quot;</span>,  </span><br><span class="line">  <span class="string">&quot;--font-size-small-s&quot;</span>: <span class="string">&quot;10px&quot;</span>, </span><br><span class="line">  <span class="string">&quot;--font-size-small&quot;</span>: <span class="string">&quot;12px&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//浅色</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> lightTheme = &#123; </span><br><span class="line">  <span class="string">&quot;--fill-1&quot;</span>: <span class="string">&quot;#fff&quot;</span>,</span><br><span class="line">  <span class="string">&quot;--text&quot;</span>: <span class="string">&quot;#3c3c3c&quot;</span>,</span><br><span class="line">  <span class="string">&quot;--text-1&quot;</span>: <span class="string">&quot;#757575&quot;</span>, </span><br><span class="line">  <span class="string">&quot;--text-2&quot;</span>: <span class="string">&quot;#222&quot;</span>,</span><br><span class="line">  ...baseSize,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 深色</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> darkTheme = &#123;</span><br><span class="line">  <span class="string">&quot;--fill-1&quot;</span>: <span class="string">&quot;#222&quot;</span>, </span><br><span class="line">  <span class="string">&quot;--text&quot;</span>: <span class="string">&quot;#fff&quot;</span>,</span><br><span class="line">  <span class="string">&quot;--text-1&quot;</span>: <span class="string">&quot;rgba(255, 255, 255, 0.3)&quot;</span>, </span><br><span class="line">  <span class="string">&quot;--text-2&quot;</span>: <span class="string">&quot;#ffcd32&quot;</span>,</span><br><span class="line">  ...baseSize,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>页面使用css变量，例如：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">lang</span>=<span class="string">&quot;scss&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css"> <span class="selector-class">.text</span> &#123;</span></span><br><span class="line"><span class="css">   <span class="attribute">display</span>: inline-block;</span></span><br><span class="line"><span class="css">   <span class="attribute">vertical-align</span>: top;</span></span><br><span class="line"><span class="css">   <span class="attribute">line-height</span>: <span class="number">70px</span>;</span></span><br><span class="line"><span class="css">   <span class="attribute">font-size</span>: <span class="built_in">var</span>(--font-size-large);  </span></span><br><span class="line"><span class="css">   <span class="attribute">color</span>: <span class="built_in">var</span>(--text-<span class="number">2</span>);  </span></span><br><span class="line"><span class="css"> &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>安装css-vars-ponyfill 插件</p>
<blockquote>
<p><strong>css-vars-ponyfill</strong>官方概念：在传统浏览器和现代浏览器中为CSS自定义属性(又名“CSS变量”)提供客户端支持的ponyfill。（具体用法与概念请查阅官方网站：<strong>css-vars-ponyfill</strong>）</p>
</blockquote>
<p>封装切换主题的js，在main.js做初始化调用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// theme.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; lightTheme, darkTheme &#125; <span class="keyword">from</span> <span class="string">&quot;../src/assets/js/variable&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> cssVars <span class="keyword">from</span> <span class="string">&quot;css-vars-ponyfill&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> initTheme = <span class="function">(<span class="params">theme</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">document</span>.documentElement.setAttribute(<span class="string">&quot;data-theme&quot;</span>, theme ? <span class="string">&quot;light&quot;</span> : <span class="string">&quot;dark&quot;</span>);</span><br><span class="line">  cssVars(&#123; </span><br><span class="line">    <span class="attr">watch</span>: <span class="literal">true</span>, <span class="comment">// 当添加，删除或修改其&lt;link&gt;或&lt;style&gt;元素的禁用或href属性时，ponyfill将自行调用</span></span><br><span class="line">    <span class="attr">variables</span>: theme ? lightTheme : darkTheme, <span class="comment">// variables 自定义属性名/值对的集合</span></span><br><span class="line">    <span class="attr">onlyLegacy</span>: <span class="literal">false</span>, <span class="comment">// false  默认将css变量编译为浏览器识别的css样式  true 当浏览器不支持css变量的时候将css变量编译为识别的css </span></span><br><span class="line">  &#125;);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091349852.webp" alt="图片">在切换主题的按钮组件中调用<img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091349207.webp" alt="图片"></p>
<p>总结：css自定义属性 + css-vars-ponyfill(解决兼容性) 预览效果<img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091350408.gif" alt="图片"></p>
<blockquote>
<p>细心的小伙伴们，一定发现了这里的css变量已经编译成浏览器可识别的css样式了。</p>
</blockquote>
<h3 id="ElementUI实现"><a href="#ElementUI实现" class="headerlink" title="ElementUI实现"></a>ElementUI实现</h3><hr>
<p>官方的实现解释</p>
<ul>
<li><p>先把默认主题文件中涉及到颜色的 CSS 值替换成关键词：github.com/ElementUI/t…</p>
</li>
<li><p>根据用户选择的主题色生成一系列对应的颜色值：github.com/ElementUI/t…</p>
</li>
<li><p>把关键词再换回刚刚生成的相应的颜色值：github.com/ElementUI/t…</p>
</li>
<li><p>直接在页面上加 <code>style</code> 标签，把生成的样式填进去：github.com/ElementUI/t…</p>
</li>
</ul>
<p>已实现的链接参考：<a target="_blank" rel="noopener" href="https://juejin.im/post/5ca41617f265da3092006155#heading-1">https://juejin.im/post/5ca41617f265da3092006155#heading-1</a></p>
<h3 id="less在线编译实现"><a href="#less在线编译实现" class="headerlink" title="less在线编译实现"></a>less在线编译实现</h3><hr>
<p>根据<strong>less</strong>可以直接 编译<strong>less变量</strong>实现的步骤如下：</p>
<figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// variable.less 定义less变量</span></span><br><span class="line"><span class="comment">// 公共字体</span></span><br><span class="line"><span class="variable">@font-size-large-x:</span> <span class="number">22px</span>;</span><br><span class="line"><span class="variable">@font-size-large:</span> <span class="number">18px</span>;</span><br><span class="line"><span class="variable">@font-size-medium:</span> <span class="number">14px</span>;</span><br><span class="line"><span class="variable">@font-size-medium-x:</span> <span class="number">16px</span>;</span><br><span class="line"><span class="variable">@font-size-small-s:</span> <span class="number">10px</span>;</span><br><span class="line"><span class="variable">@font-size-small:</span> <span class="number">12px</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 浅色</span></span><br><span class="line"><span class="variable">@fill-1:</span> <span class="number">#fff</span>;</span><br><span class="line"><span class="variable">@text:</span> <span class="number">#3c3c3c</span>;</span><br><span class="line"><span class="variable">@text-1:</span> <span class="number">#757575</span>;</span><br><span class="line"><span class="variable">@text-2:</span> <span class="number">#222</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 页面使用 例如：</span></span><br><span class="line"><span class="comment">// 下面.text的css 如下，这里的 @font-size-large 和  @text-2就是 less 变量：</span></span><br><span class="line"><span class="selector-class">.text</span> &#123; </span><br><span class="line">  <span class="attribute">display</span>: inline-block;  </span><br><span class="line">  <span class="attribute">vertical-align</span>: top;  </span><br><span class="line">  <span class="attribute">line-height</span>: <span class="number">70px</span>; </span><br><span class="line">  <span class="attribute">font-size</span>: <span class="variable">@font-size-large</span>;  </span><br><span class="line">  <span class="attribute">color</span>: <span class="variable">@text-2</span>;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当点击换肤按钮的时候，直接去加载 <code>less.js</code>，具体代码如下:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;header&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;text&quot;</span>&gt;</span>小恐龙换肤<span class="tag">&lt;/<span class="name">div</span>&gt;</span> </span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">role</span>=<span class="string">&quot;switch&quot;</span> <span class="attr">class</span>=<span class="string">&quot;switch&quot;</span> <span class="attr">:class</span>=<span class="string">&quot;theme === true ? &#x27;is-checked&#x27; : &#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">class</span>=<span class="string">&quot;switch-input&quot;</span> /&gt;</span>  </span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;switch-core&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;changeTheme&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span>   </span><br><span class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span> </span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="keyword">import</span> &#123; lightTheme, darkTheme &#125; <span class="keyword">from</span> <span class="string">&quot;../../assets/js/variable&quot;</span>;</span></span><br><span class="line"><span class="javascript"> <span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">   <span class="attr">name</span>: <span class="string">&quot;m-header&quot;</span>,  </span></span><br><span class="line"><span class="javascript">   <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">     <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">       <span class="attr">theme</span>: <span class="literal">true</span>   </span></span><br><span class="line"><span class="javascript">      &#125;;  </span></span><br><span class="line"><span class="javascript">   &#125;, </span></span><br><span class="line"><span class="javascript">   <span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="javascript">     <span class="function"><span class="title">changeTheme</span>(<span class="params"></span>)</span> &#123;   </span></span><br><span class="line"><span class="javascript">       <span class="built_in">this</span>.theme = !<span class="built_in">this</span>.theme;      <span class="comment">// 调用 `less.modifyVars` 方法来改变变量值      </span></span></span><br><span class="line"><span class="javascript">       <span class="built_in">window</span>.less.modifyVars(<span class="built_in">this</span>.theme ? lightTheme : darkTheme);   </span></span><br><span class="line"><span class="javascript">     &#125;</span></span><br><span class="line"><span class="javascript">   &#125;, </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span> &#123;&#125;</span></span><br><span class="line"><span class="javascript"> &#125;;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>定义variable.js是因为如果直接将less变量放在modifyVars中切换的效果只会生效一次，所以根据切换的状态使用对应的less变量。</p>
<figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//浅色</span></span><br><span class="line"><span class="selector-tag">export</span> <span class="selector-tag">const</span> <span class="selector-tag">lightTheme</span> = &#123;</span><br><span class="line"> &quot;<span class="variable">@fill-1</span><span class="string">&quot;: &quot;</span><span class="number">#fff</span>&quot;,  </span><br><span class="line">  <span class="string">&quot;@text&quot;</span>: <span class="string">&quot;#3c3c3c&quot;</span>, </span><br><span class="line">  <span class="string">&quot;@text-1&quot;</span>: <span class="string">&quot;#757575&quot;</span>, </span><br><span class="line">  <span class="string">&quot;@text-2&quot;</span>: <span class="string">&quot;#222&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 深色</span></span><br><span class="line"><span class="selector-tag">export</span> <span class="selector-tag">const</span> <span class="selector-tag">darkTheme</span> = &#123; </span><br><span class="line"> &quot;<span class="variable">@fill-1</span><span class="string">&quot;: &quot;</span><span class="number">#222</span>&quot;,  </span><br><span class="line"> <span class="string">&quot;@text&quot;</span>: <span class="string">&quot;#fff&quot;</span>, </span><br><span class="line"> <span class="string">&quot;@text-1&quot;</span>: <span class="string">&quot;rgba(255, 255, 255, 0.3)&quot;</span>, </span><br><span class="line"> <span class="string">&quot;@text-2&quot;</span>: <span class="string">&quot;#ffcd32&quot;</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>然后点击色块进行试验，发现并没有生效，这是why？然后就去看了其文档，原来它会找到所有如下的less 样式标签，并且使用已编译的css同步创建 style 标签。也就是说我们必须吧代码中所有的less 都以下面这种link的方式来引入，这样<code>less.js</code> 才能在浏览器端实现编译。</p>
<blockquote>
<p><code>&lt;link rel=&quot;stylesheet/less&quot; type=&quot;text/css&quot; href=&quot;index.less&quot; /&gt;</code></p>
</blockquote>
<p>这里我用了vue，所以直接把 <code>less</code> 文件放在了<code>public</code>目录下，然后在html中直接引入：<img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091356761.webp" alt="图片"><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091356521.webp" alt="图片"></p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091356606.webp" alt="图片">点击切换按钮，可见background和color确实都变了<img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091356093.webp" alt="图片"><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091357342.webp" alt="图片"></p>
<blockquote>
<p>注：使用less 来实现换肤要注意 <code>less 文件</code>在 <code>html</code> 中编写的位置，不然很可能被其他css 文件所干扰导致换肤失败。如果less文件特别大，会存在编译性能问题。</p>
</blockquote>
<h3 id="拓展-图片切换"><a href="#拓展-图片切换" class="headerlink" title="拓展-图片切换"></a>拓展-图片切换</h3><hr>
<p>以上的方案—只是对background-color和color进行的换肤，如果要对图片进行换肤该怎么办呐？                                     <img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091357214.webp" alt="图片"></p>
<h4 id="图片切换"><a href="#图片切换" class="headerlink" title="图片切换"></a><strong>图片切换</strong></h4><p>项目中还存在很多占位图或者其他图片会随着主题的变化而变化</p>
<p>通过引入所有图片，并用文件名来区分不同主题所对应的图片</p>
<p>在点击切换主题时，切换到主题所对应的文件，就能实现图片切换了</p>
<blockquote>
<p><code>&lt;img class :src=&quot;avatar&quot; alt /&gt;</code></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">// 页面实现</span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;header&quot;</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;text&quot;</span>&gt;</span>小恐龙换肤<span class="tag">&lt;/<span class="name">div</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">role</span>=<span class="string">&quot;switch&quot;</span> <span class="attr">class</span>=<span class="string">&quot;switch&quot;</span> <span class="attr">:class</span>=<span class="string">&quot;theme === true ? &#x27;is-checked&#x27; : &#x27;&#x27;&quot;</span>&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">class</span>=<span class="string">&quot;switch-input&quot;</span> /&gt;</span>    </span><br><span class="line">      <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;switch-core&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;changeTheme&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span> </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">  <span class="keyword">import</span> &#123; initTheme &#125; <span class="keyword">from</span> <span class="string">&quot;../../theme&quot;</span>;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">import</span> bus <span class="keyword">from</span> <span class="string">&quot;../../bus&quot;</span>;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">export</span> <span class="keyword">default</span> &#123; </span></span><br><span class="line"><span class="javascript">    <span class="attr">name</span>: <span class="string">&quot;m-header&quot;</span>, </span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">        <span class="attr">theme</span>: <span class="literal">true</span>, <span class="comment">// false深色主题 </span></span></span><br><span class="line"><span class="javascript">        <span class="attr">avatar</span>: <span class="string">&quot;&quot;</span> </span></span><br><span class="line"><span class="javascript">      &#125;;</span></span><br><span class="line"><span class="javascript">    &#125;,</span></span><br><span class="line"><span class="javascript">   <span class="attr">methods</span>: &#123;   </span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">changeTheme</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">     <span class="built_in">this</span>.theme = !<span class="built_in">this</span>.theme;    </span></span><br><span class="line"><span class="javascript">     initTheme(<span class="built_in">this</span>.theme);</span></span><br><span class="line"><span class="javascript">     <span class="built_in">this</span>.setThemeValue(<span class="built_in">this</span>.theme);</span></span><br><span class="line"><span class="javascript">     bus.$emit(<span class="string">&quot;changeTheme&quot;</span>, <span class="built_in">this</span>.theme);   </span></span><br><span class="line"><span class="javascript">    &#125;,</span></span><br><span class="line"><span class="javascript">    </span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">setThemeValue</span>(<span class="params">theme</span>)</span> &#123;    </span></span><br><span class="line"><span class="javascript">     theme = theme ? <span class="string">&quot;light&quot;</span> : <span class="string">&quot;dark&quot;</span>;</span></span><br><span class="line"><span class="javascript">     <span class="built_in">this</span>.avatar = <span class="built_in">require</span>(<span class="string">`@/assets/images/logo-<span class="subst">$&#123;theme&#125;</span>.jpeg`</span>);</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">   &#125;, </span></span><br><span class="line"><span class="javascript">   </span></span><br><span class="line"><span class="javascript">   <span class="function"><span class="title">created</span>(<span class="params"></span>)</span> &#123;    <span class="built_in">this</span>.setThemeValue(<span class="built_in">this</span>.theme);  &#125;&#125;;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在点击切换主题时，会发射一个 <code>changeTheme</code> 事件，各组件接收到 <code>changeTheme</code> 事件，就会为图片重新赋值，也就达到了切换图片的效果。</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110091400731.gif" alt="图片"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog">Vixcity</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog/2021/10/09/%E3%80%90%E5%89%8D%E7%AB%AF%E6%8D%A2%E8%82%A4%E3%80%91%E5%89%8D%E7%AB%AF%E6%8D%A2%E8%82%A4%E7%9A%84N%E7%A7%8D%E6%96%B9%E6%A1%88%EF%BC%8C%E8%AF%B7%E6%94%B6%E4%B8%8B/">http://vixcity.gitee.io/my-gitee-blog/2021/10/09/%E3%80%90%E5%89%8D%E7%AB%AF%E6%8D%A2%E8%82%A4%E3%80%91%E5%89%8D%E7%AB%AF%E6%8D%A2%E8%82%A4%E7%9A%84N%E7%A7%8D%E6%96%B9%E6%A1%88%EF%BC%8C%E8%AF%B7%E6%94%B6%E4%B8%8B/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://vixcity.gitee.io/my-gitee-blog" target="_blank">Vixcity</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-gitee-blog/tags/HTML/">HTML</a><a class="post-meta__tags" href="/my-gitee-blog/tags/%E5%89%8D%E7%AB%AF%E6%8D%A2%E8%82%A4/">前端换肤</a></div><div class="post_share"><div class="social-share" data-image="/my-gitee-blog/img/%E8%9C%82%E7%AA%9D%E5%BD%A2%E5%85%AD%E8%BE%B9%E5%BD%A2%E8%93%9D%E7%B4%AB%E8%89%B2%E8%B0%83%E7%82%AB%E9%85%B7%E8%83%8C%E6%99%AF.jpg" data-sites="wechat,qq"></div><link rel="stylesheet" href="/my-gitee-blog/" media="print" onload="this.media='all'"><script src="/my-gitee-blog/" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-gitee-blog/2021/10/11/%E3%80%90CSS%E3%80%9150%E4%B8%AA%E6%9C%89%E4%BB%B7%E5%80%BC%E7%9A%84CSS%E7%BC%96%E5%86%99%E8%A7%84%E5%88%99%EF%BC%8C%E8%AE%A9%E4%BD%A0%E5%86%99%E5%87%BA%E6%9B%B4%E5%A5%BD%E7%9A%84CSS/"><img class="prev-cover" src="/my-gitee-blog/img/CSS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【CSS】50个有价值的CSS编写规则，让你写出更好的CSS</div></div></a></div><div class="next-post pull-right"><a href="/my-gitee-blog/2021/10/04/%E3%80%90CSS%E3%80%91CSS%20%E4%BB%A3%E7%A0%81%E9%87%8D%E6%9E%84%E4%B8%8E%E4%BC%98%E5%8C%96%E4%B9%8B%E8%B7%AF/"><img class="next-cover" src="/my-gitee-blog/img/CSS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【CSS】CSS 代码重构与优化之路</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/my-gitee-blog/2021/09/23/%E3%80%90HTML%E3%80%91%E5%A6%82%E4%BD%95%E8%AE%BE%E7%BD%AEDIV%E5%8F%AF%E7%BC%96%E8%BE%91/" title="【HTML】如何设置DIV可编辑"><img class="cover" src="/my-gitee-blog/img/HTML.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-23</div><div class="title">【HTML】如何设置DIV可编辑</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81NDQ0NS8zMDkxNg=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-gitee-blog/img/avatar.png" onerror="this.onerror=null;this.src='/my-gitee-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Vixcity</div><div class="author-info__description">今日事，今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Vixcity"><i class="fab fa-github"></i><span>我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Vixcity" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2091283625@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是Vixcity目前的小博客</div></div><div class="sticky_layout"><div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden;"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%;"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS 服务端请求"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求">【NodeJS】NodeJS 服务端请求</a><time datetime="2022-07-12T16:00:00.000Z" title="发表于 2022-07-13 00:00:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 1"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1">【Hexo】标签样式列表参考 - 1</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 2"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2">【Hexo】标签样式列表参考 - 2</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 3"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3">【Hexo】标签样式列表参考 - 3</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS入门学习"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习">【NodeJS】NodeJS入门学习</a><time datetime="2022-07-06T16:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-gitee-blog/img/%E8%9C%82%E7%AA%9D%E5%BD%A2%E5%85%AD%E8%BE%B9%E5%BD%A2%E8%93%9D%E7%B4%AB%E8%89%B2%E8%B0%83%E7%82%AB%E9%85%B7%E8%83%8C%E6%99%AF.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Vixcity</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div id="running-time"></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-gitee-blog/js/utils.js"></script><script src="/my-gitee-blog/js/main.js"></script><script src="/my-gitee-blog/js/tw_cn.js"></script><script src="/my-gitee-blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/my-gitee-blog/js/newStyle.js"></script><script src="/my-gitee-blog/js/sakura.js"></script><script>setInterval(()=>{let create_time=Math.round(new Date("2021-9-8-00:00:00").getTime()/1000);let timestamp=Math.round((new Date().getTime()+8*60*60*1000)/1000);let second=timestamp-create_time;let time=new Array(0,0,0,0,0);if(second>=365*24*3600){time[0]=parseInt(second/(365*24*3600));second%=365*24*3600}if(second>=24*3600){time[1]=parseInt(second/(24*3600));second%=24*3600}if(second>=3600){time[2]=parseInt(second/3600);second%=3600}if(second>=60){time[3]=parseInt(second/60);second%=60}if(second>0){time[4]=second}currentTimeHtml='小博客已经安全运行 '+time[0]+' 年 '+time[1]+' 天 '+time[2]+' 时 '+time[3]+' 分 '+time[4]+' 秒';var elementById=document.getElementById('running-time');if(elementById){elementById.innerHTML=currentTimeHtml}},1000);</script><script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script><script src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0.5s');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><!-- hexo injector body_end end --><script src="/my-gitee-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>